<template>
  <!--
       1 el-table显示数据,分页 
       2  token cookie|session 服务端保存状态信息方式   
       H5 token 如果有值 登录状态 否则就是登录
       进入某个页面就要进行拦截：判断用户是否登录
       通过什么方式：路由守卫
       3 添加和更新
  -->


  <div class="categorys">
    <div class="category-tools">
      <el-button type="warning" @click="toAdd">添加</el-button>
    </div>
    <el-table :data="categorys" style="width: 100%">
      <el-table-column prop="id" label="编号" width="80" />
      <el-table-column prop="name" label="名称" width="120" />
      <el-table-column prop="icon" label="分类图标" width="120" />
      <el-table-column prop="subject_count" label="专题数量" width="120" />
      <el-table-column prop="show_status" label="显示状态" width="120" />
      <el-table-column prop="sort" label="种类" />

      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="toEdit(scope.row)">更新</el-button>
          <el-button link type="primary" size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination layout="prev, pager, next" :page-size="page.size" :total="page.total"
      @current-change="currentchange" />
  </div>
  <el-dialog v-model="dialogFormVisible" title="种类编辑">
    <el-form :model="adCate">
      <el-form-item label="种类编号" :label-width="formLabelWidth">
        <el-input v-model="category.id" autocomplete="off" />
      </el-form-item>
      <el-form-item label="名称" :label-width="formLabelWidth">
        <el-input v-model="category.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="分类图标" :label-width="formLabelWidth">
        <el-input v-model="category.icon" autocomplete="off" />
      </el-form-item>
      <el-form-item label="专题数量" :label-width="formLabelWidth">
        <el-input v-model="category.subject_count" autocomplete="off" />
      </el-form-item>
      <el-form-item label="显示状态" :label-width="formLabelWidth">
        <el-input v-model="category.show_status" autocomplete="off" />
      </el-form-item>
      <el-form-item label="种类" :label-width="formLabelWidth">
        <el-input v-model="category.sort" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="save">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { defineComponent } from "vue"
import { cms_subject_categoryPage, cms_subject_categoryDelId, cms_subject_categoryAdd, cms_subject_categoryEdit } from "../../http/cms_subject_category.js";
import { ElMessage } from 'element-plus'
import { cloneDeep } from 'lodash-es'
export default defineComponent({
  data() {
    return {
      categorys: [],
      page: {
        total: 0,
        current: 1,
        size: 10
      },
      dialogFormVisible: false,
      category: {
        'id': 0,
        'name': '',
        'icon': '',
        'subject_count': '',
        'show_status': '',
        'sort': ''
      },
      formLabelWidth: 80
    }
  },
  mounted() {
    this.getCateGorysPage(1)
  },
  methods: {
    toEdit(category) {
      console.log(category);
      this.dialogFormVisible = true;
      this.category = cloneDeep(category);
    },
    getCateGorysPage(current) {
      const data = {
        current: current,
        size: 2
      }
     cms_subject_categoryPage(data).then(res => {
        console.log(res);
        const page = res.data.page;
        this.categorys = page.records;
        this.page = page;
      }).catch(err => {
        console.log(err);
      })

    },
    currentchange(current) {
      // console.log(current);
      this.getCateGorysPage(current);
      this.page.current = current;//数据更新目前显示的页面
    },
    del(id) {
      //删除数据
      //模拟删除，服务器进行处理 
      //要别人反悔机会
      // 如果只有一条数据，删除数据之后如何处理？
      //人机交互
      console.log(id)
      const params = {
        id: id
      }
      cms_subject_categoryDelId(params).then(res => {
        if (res.success) {
          this.getCateGorysPage(this.page.current)

        }
        else {
          console.log(res.msg)
          return false
        }


      }).catch(err => {

      })
    },
    toAdd() {
      //到添加的页面
      this.dialogFormVisible = true;
    },
    save() {
      const cateGory = this.category;
      if (cateGory.id == 0) {
        cms_subject_categoryAdd(cateGory).then(res => {
          if (res.success) {
            //刷新页面
            this.dialogFormVisible = false;
            this.getCateGorysPage(this.page.current)
            ElMessage(res.msg)
          }
          else {
            ElMessage(res.msg)
            return false
          }
        }).catch(err => {
          ElMessage('网络错误联系管理员')
        })
      }
      else {
        cms_subject_categoryEdit(cateGory).then(res => {
          if (res.success) {
            //刷新页面
            this.dialogFormVisible = false;
            this.getCateGorysPage(this.page.current)
            ElMessage(res.msg)
          }
          else {
            ElMessage(res.msg)
            return false
          }
        }).catch(err => {
          ElMessage('网络错误联系管理员')
        })
      }

    }

  }

});
</script>

<style lang="scss" scoped></style>
